{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test the granger casuality between temp and activity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Import finished!\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import datetime\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sb\n",
    "from statsmodels.tsa.api import VAR\n",
    "from statsmodels.tsa.stattools import adfuller\n",
    "\n",
    "print (\"Import finished!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sizeof_fmt(num, suffix='B'):\n",
    "    '''output data sizes in a nice way, i.e. B, KiB, MiB, GiB, ...\n",
    "    by Fred Cirera,  https://stackoverflow.com/a/1094933/1870254,\n",
    "    modified'''\n",
    "    for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:\n",
    "        if abs(num) < 1024.0:\n",
    "            return \"%3.1f %s%s\" % (num, unit, suffix)\n",
    "        num /= 1024.0\n",
    "    return \"%.1f %s%s\" % (num, 'Yi', suffix)\n",
    "\n",
    "def readMiceData(batch, name):\n",
    "    print('{}/batch{}/{}.Activity.txt'.format(path, batch, name))\n",
    "    print('{}/batch{}/{}.Temperature.txt'.format(path, batch, name))\n",
    "    a = np.genfromtxt('{}/batch{}/{}.Activity.txt'.format(path, batch, name))\n",
    "    b = np.genfromtxt('{}/batch{}/{}.Temperature.txt'.format(path, batch, name))\n",
    "\n",
    "    if a[0,0] != b[0,0]:\n",
    "        start = max(a[0,0], b[0,0])\n",
    "    else:\n",
    "        start = a[0,0]\n",
    "    if a[-1,0] != b[-1,0]:\n",
    "        end = min(a[-1,0], b[-1,0])\n",
    "    else:\n",
    "        end = a[-1,0]\n",
    "\n",
    "    a = a[(a[:,0]<=end)]\n",
    "    a = a[(a[:,0]>=start)]\n",
    "    b = b[(b[:,0]<=end)]\n",
    "    b = b[(b[:,0]>=start)]\n",
    "    \n",
    "    return a, b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Function to test the stationary of data\n",
    "    checkStat will check the stationarity of data and return False if the data is not stationary even though the order is 3 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def checkStat(data, order, alpha=0.05):\n",
    "    stat_p = adfuller(data, regression = 'ctt', maxlag = order, autolag = None)[1]\n",
    "    print (f'Order: {order}, p_val: {stat_p}')\n",
    "    if stat_p < alpha:\n",
    "        return True\n",
    "    else:\n",
    "        if order < 3:\n",
    "            return False\n",
    "        else: \n",
    "            return checkStat(data, order-1, alpha)\n",
    "        \n",
    "# just to test the logic of resurse function\n",
    "def recurse(num):\n",
    "    print(num)\n",
    "    if num < 1:\n",
    "        return True\n",
    "    else:\n",
    "        return recurse(num-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Readin the metadata of mice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./batch3/12Otx2.Activity.txt\n",
      "./batch3/12Otx2.Temperature.txt\n",
      "./batch3/56Otx2.Activity.txt\n",
      "./batch3/56Otx2.Temperature.txt\n",
      "./batch3/73Otx2.Activity.txt\n",
      "./batch3/73Otx2.Temperature.txt\n",
      "./batch3/84Otx2.Activity.txt\n",
      "./batch3/84Otx2.Temperature.txt\n",
      "./batch3/82WT.Activity.txt\n",
      "./batch3/82WT.Temperature.txt\n",
      "./batch3/83WT.Activity.txt\n",
      "./batch3/83WT.Temperature.txt\n",
      "./batch5/51M.Activity.txt\n",
      "./batch5/51M.Temperature.txt\n",
      "./batch5/52M.Activity.txt\n",
      "./batch5/52M.Temperature.txt\n",
      "./batch5/54M.Activity.txt\n",
      "./batch5/54M.Temperature.txt\n",
      "./batch5/56M.Activity.txt\n",
      "./batch5/56M.Temperature.txt\n",
      "./batch5/58M.Activity.txt\n",
      "./batch5/58M.Temperature.txt\n",
      "./batch5/53.Activity.txt\n",
      "./batch5/53.Temperature.txt\n",
      "./batch5/55.Activity.txt\n",
      "./batch5/55.Temperature.txt\n",
      "./batch5/57.Activity.txt\n",
      "./batch5/57.Temperature.txt\n",
      "./batch6/68M.Activity.txt\n",
      "./batch6/68M.Temperature.txt\n",
      "./batch6/62M.Activity.txt\n",
      "./batch6/62M.Temperature.txt\n",
      "./batch6/64M.Activity.txt\n",
      "./batch6/64M.Temperature.txt\n",
      "./batch6/61.Activity.txt\n",
      "./batch6/61.Temperature.txt\n",
      "./batch6/63.Activity.txt\n",
      "./batch6/63.Temperature.txt\n",
      "./batch6/65.Activity.txt\n",
      "./batch6/65.Temperature.txt\n",
      "./batch6/67.Activity.txt\n",
      "./batch6/67.Temperature.txt\n"
     ]
    }
   ],
   "source": [
    "metaData = pd.read_csv('mice')\n",
    "path = '.'\n",
    "for a in metaData.index:\n",
    "    batch = metaData.loc[a, 'batch']\n",
    "    name = metaData.loc[a, 'mice']\n",
    "    act, temp = readMiceData(batch, name)\n",
    "    #print('{}/batch{}/{}.Temperature.txt'.format(path, batch, name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
